package com.lw.nacos.client.service.impl;

import com.lw.nacos.client.pojo.Stock;
import com.lw.nacos.client.repositories.StockRepository;
import com.lw.nacos.client.service.StockService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@GlobalTransactional
public class StockServiceImpl implements StockService{
    @Autowired
    private   StockRepository stockRepository;
    @Override
    public Stock deduct(Long productId, Integer count)
    {
        stockRepository.findById(productId).ifPresent(
                stock -> {stock.setCount(stock.getCount()-count);
                stockRepository.save(stock);}
        );
        return stockRepository.findById(productId).get();
    }
}
